package com.fengpanlong.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.fengpanlong.entity.Document;
import com.fengpanlong.util.DBHelp;
import com.fengpanlong.util.RowMapper;

public class DocumentDao {
	private DBHelp<Document> db = new DBHelp<Document>();
	public List<Document> findAll(String projectid){
		String sql = "select id,title,content,createtime,employeeid,projectid from t_document where projectid=?";
		return db.executeQueryForList(sql,new DocumentRowMapper(),projectid);
	}
	public void delete(String id ){
		String sql = "delete from t_document where id= ?";
		db.executeSQL(sql,id);
	}
	
	public void edit(Document document){
		String sql="update t_document set title=?,content=? where id=?";
		db.executeSQL(sql, document.getTitle(),document.getContent(),document.getId());
	}
	
	public Document findById(String id) {
		String sql="select id,title,content,createtime,employeeid,projectid from t_document where id=?";
		return db.executeQueryForObject(sql,new DocumentRowMapper(),id);
	}
	public void save(Document document){
		String sql = "insert into t_document(id,title,content,createtime,employeeid,projectid) values(?,?,?,?,?,?)";
		db.executeSQL(sql,document.getId(),document.getTitle(),document.getContent(),document.getCreatetime(),document.getEmployeeId(),document.getProjectId());
	}
	private class DocumentRowMapper implements RowMapper<Document> {
		public Document mapperRow(ResultSet rs) throws SQLException {
			Document d = new Document();
			d.setId(rs.getString("id"));
			d.setTitle(rs.getString("title"));
			d.setContent(rs.getString("content"));
			d.setCreatetime(rs.getString("createtime"));
			d.setProjectId(rs.getString("projectId"));
			d.setEmployeeId(rs.getString("employeeId"));
			return d;
		}
	}
}
